package com.he.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.he.pojo.Step;
import com.he.pojo.StepExample;
import com.he.pojo.StepExample.Criteria;
import com.he.pojo.Workflow;
import com.he.service.step.StepServiceI;
import com.he.service.workflow.WorkflowServiceI;

@Controller
@RequestMapping("workFlow")
public class WorkFlowController {
	
	@Autowired
	private WorkflowServiceI wfi;
	@Autowired
	private StepServiceI ssi;
	
	@RequestMapping("tomain")//返回主页
	public String toMain(){
		return "index";
	}
	
	@RequestMapping("show")//显示所有流程
	public ModelAndView showall(Workflow workflow){
		List<Workflow> list=wfi.getWorkflows(null, workflow);
		for (int i = 0; i < list.size(); i++) {
			StepExample stepExample=new StepExample();
			Step step=new Step();
			Criteria createCriteria = stepExample.createCriteria();
			createCriteria.andWorkflowIdEqualTo(list.get(i).getId());
			List<Step> steps=ssi.getSteps(stepExample, step);
			Integer stepnum=steps.size();
			list.get(i).setStepnum(stepnum);		
		}
		ModelAndView mv=new ModelAndView("flowmanage");
		mv.addObject("list", list);
		/*为workflow总页数赋值,向前端传总页数*/
		PageInfo<Workflow> info=new PageInfo<>(list);
		//为workflow总页数赋值
		workflow.setPages(info.getPages());
		//为workflow总记录数赋值
		workflow.setTotal(info.getTotal());
		/*为workflow返回的controller链接赋值*/
		workflow.setUrl("show.do");
		/*向前端传参数保存在workflow中*/
		mv.addObject("workflow2", workflow);
		return mv;
	}
	@RequestMapping("addflow")
	@ResponseBody
	public String add(Workflow workflow){
		int r=0;
		r=wfi.insert(workflow);
		if (r>0) {
			return "1";
		} else {
			return "0";
		}		
	}
	
	@RequestMapping("editflow")
	@ResponseBody
	public String edit(Workflow workflow){
		int r=0;
		//System.out.println(workflow.getId());
		r=wfi.update(workflow);
		if (r>0) {
			return "1";
		} else {
			return "0";
		}		
	}
	@RequestMapping("deletflow")
	@ResponseBody
	public String delete(Integer id){
		int r=wfi.delete(id);
		if (r>0) {
			return "1";
		} else {
			return "0";
		}
	}
}
